package com.coding.controller;

import com.coding.base.pojo.BaseModel;
import com.coding.pojo.WrongQuestion;
import com.coding.service.WrongQuestionService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @auther wangchenghao
 * @date 2020/10/3
 **/
@Controller
@ResponseBody
@RequestMapping("wrongQuestion")
public class WrongQuestionController {
    @Autowired
    WrongQuestionService wrongQuestionService;

    @ApiOperation(value = "获得用户错题")
    @RequestMapping(value = "/getList", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel getList(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) int user_id) {
        BaseModel baseModel = new BaseModel();
        try {
            wrongQuestionService.selectByUser(user_id, baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }

    @ApiOperation(value = "获得用户单个错题")
    @RequestMapping(value = "/getSingle", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel getSingle(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) int user_id,
                               @ApiParam("题目id") @RequestParam(value = "question_id", required = true) int question_id) {
        BaseModel baseModel = new BaseModel();
        try {
            wrongQuestionService.selectById(question_id, user_id, baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }

    @ApiOperation(value = "新增用户错题")
    @RequestMapping(value = "/insertWrong", method = {RequestMethod.POST})
    @ResponseBody
    public BaseModel insertWrong(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) int user_id,
                                 @ApiParam("题目id") @RequestParam(value = "question_id", required = true) int question_id,
                                 @ApiParam("错误选项") @RequestParam(value = "wrong_choose", required = true) String wrong_choose,
                                 @ApiParam("错误内容") @RequestParam(value = "wrong_content", required = true) String wrong_content
    ) {
        BaseModel baseModel = new BaseModel();
        try {
            WrongQuestion wrongQuestion = new WrongQuestion();
            wrongQuestion.setUser_id(user_id);
            wrongQuestion.setQuestion_id(question_id);
            wrongQuestion.setFalse_choose(wrong_choose);
            wrongQuestion.setFalse_content(wrong_content);
            wrongQuestionService.insertWrong(wrongQuestion, baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }

    @ApiOperation(value = "删除用户错题")
    @RequestMapping(value = "/deleteWrong", method = {RequestMethod.DELETE})
    @ResponseBody
    public BaseModel deleteWrong(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) int user_id,
                                 @ApiParam("题目id") @RequestParam(value = "question_id", required = true) int question_id
    ) {
        BaseModel baseModel = new BaseModel();
        try {
            wrongQuestionService.deleteWrong(user_id, question_id, baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }
}
